package com.tj.view.tool;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;

import static android.content.ContentValues.TAG;

/**
 * dp、sp 转换为 px 的工具类
 *
 * Created by sunfusheng on 16/5/31.
 */
public class DisplayUtil {

    // 将px值转换为dip或dp值，保证尺寸大小不变
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    // 将dip或dp值转换为px值，保证尺寸大小不变
    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    // 将px值转换为sp值，保证文字大小不变
    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    // 将sp值转换为px值，保证文字大小不变
    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    // 屏幕宽度（像素）
    public static int getWindowWidth(Activity context) {
        DisplayMetrics metric = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(metric);
        return metric.widthPixels;
    }

    // 屏幕高度（像素）
    public static int getWindowHeight(Activity context) {
        DisplayMetrics metric = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(metric);
        return metric.heightPixels;
    }

    //屏幕密度
    public static float getWindowDensity(Activity context){
        DisplayMetrics metrics = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        return metrics.density;
    }

    //屏幕密度
    public static float getWindowDensityDpi(Activity context){
        DisplayMetrics metrics = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        return metrics.densityDpi;
    }

    /**
     * 截图activity,不带电量显示的一栏
     *
     * 通过cache机制保存为bitmap
     * @param activity
     * @return
     */
    public static Bitmap getDisplayShot(Activity activity){

        //window中最顶层的view
        View view = activity.getWindow().getDecorView();
        //???
        view.buildDrawingCache();

        //屏幕的状态栏高度
        Rect rect = new Rect();
        view.getWindowVisibleDisplayFrame(rect);
        int statusHeight = rect.top;
        Log.d(TAG, "getDisplayShot: statusHeight="+statusHeight);

        //屏幕的宽和高
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int widths = metrics.widthPixels;
        int heights = metrics.heightPixels;

        //把cache开启
        view.setDrawingCacheEnabled(true);

        //view.getDrawingCache()是获得view的cache图片
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache()
                ,0,statusHeight,widths,heights-statusHeight);

        //旧的cache销毁
        view.destroyDrawingCache();
        return bitmap;
    }

    /**
     * 获取到状态栏的高度
     */
    public static int getStatusHeight(Activity activity){

        Rect rect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        int statusHeight = rect.top;
        Log.d(TAG, "getStatusHeight: statusHeight="+statusHeight);
        return statusHeight;
    }

    /**
     * 获取标题栏的高度
     * @param activity
     * @return
     */
    public static int getTitleHeight(Activity activity){

        //获得根视图
        int top = activity.getWindow()
                .findViewById(Window.ID_ANDROID_CONTENT).getTop();
        int titleHeight = top - getStatusHeight(activity);
        Log.d(TAG, "getTitleHeight: titleHeight="+titleHeight);
        return titleHeight;
    }









}